package com.ziyun.pop.web.api.sys;

import com.alibaba.fastjson.JSONObject;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import io.renren.common.utils.Base64Object;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @Author:chenxiaoyi
 * @Date:2018/10/17 11:23
 */
@RestController
public class PopMallController {
    @Value("${popIndex}")
    private String popIndex;

    @RequestMapping("/jumpMallIndex")
    public void jumpMallIndex(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SysUserEntity user = UserUtils.getCurentUser();
        String s = JsonUtils.object2String(user);
        JSONObject json = new JSONObject();
        json.put("popUserInfo", s);
        StringBuffer url = new StringBuffer(popIndex+"/toMallIndex.do");
        url.append("?token=");
        String jsonStr = URLEncoder.encode(json.toJSONString(), "UTF-8");
        url.append(Base64Object.stringToBase64(jsonStr));
        response.sendRedirect(url.toString());
    }

    @RequestMapping("/jumpMallGoodsPage")
    public void jumpMallGoodsPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SysUserEntity user = UserUtils.getCurentUser();
        String s = JsonUtils.object2String(user);
        String goodsId = request.getParameter("goodsId");
        String goodsType = request.getParameter("goodsType");
        String path = "/mall/skuProductDetails.do?goodsId="+ goodsId +"&goodsType=" + goodsType;
        JSONObject json = new JSONObject();
        json.put("popUserInfo", s);
        json.put("path", path);
        StringBuffer url = new StringBuffer(popIndex+"/toMallIndex.do");
        url.append("?token=");
        String jsonStr = URLEncoder.encode(json.toJSONString(), "UTF-8");
        url.append(Base64Object.stringToBase64(jsonStr));
        response.sendRedirect(url.toString());
    }

}
